#ifndef _SHADER_TEXTURE_H_
#define _SHADER_TEXTURE_H_

#include "./shader_util.h"

namespace ShaderSpace {

	using namespace Visual::ColorSpace;

	struct COLOR_POS {
		float pos[3];

		inline COLOR_POS& operator= ( const COLOR_POS &color ){
			pos[R] = color.pos[R];
			pos[G] = color.pos[G];
			pos[B] = color.pos[B];
			return (*this);
		};

		inline COLOR_POS& operator+= ( const COLOR_POS &color ){
			pos[R] += color.pos[R];
			pos[G] += color.pos[G];
			pos[B] += color.pos[B];
			return (*this);
		};

		inline COLOR_POS operator+ ( const COLOR_POS &color ) const {
			COLOR_POS result( *this );
			return result += color;
		};
	};

	class MAT_COLOR_POS {
	public:
		usint w, h;
		COLOR_POS *buf;

		MAT_COLOR_POS(  const usint &w, const usint &h ): w(w), h(h), buf(NULL) {
			buf = (COLOR_POS*) malloc( w*h * sizeof(COLOR_POS) );
			if( buf == NULL )
				exit(-21);
		};

		inline void setVal( const usint &i, const usint &j, const COLOR_POS &value ){
			buf[i*w+j] = value;
		};

		inline COLOR_POS getVal( const usint &i, const usint &j ){
			return buf[i*w+j];
		};

	};

	// TODO - Make a Sampler class for accessing the texture
	class ShaderTexture {
	protected:
		MAT_COLOR_POS *texture;
		ID3D11Texture2D* *texture_formated;
		ID3D11ShaderResourceView*	*texture_gpu;

	public:
		ShaderTexture();
		~ShaderTexture();

		inline MAT_COLOR_POS* getTexture() const {
			return texture;
		};

		inline void setTexture( MAT_COLOR_POS* ptr_texture ) {
			texture = ptr_texture;
		};

		inline ID3D11Texture2D*& getTextureFormated() const {
			return *texture_formated;
		};

		inline ID3D11ShaderResourceView*& getTextureGPU() const {
			return *texture_gpu;
		};

		bool loadColorMatrix( const string &file_name );

		bool createOnDevice( ID3D11Device *target_device );
		bool destroy();

		bool loadFromFile( ID3D11Device *target_device, const string &file_name );
		bool saveToFile( ID3D11DeviceContext *target_device_context, const string &file_name );

	};

};

#endif // _SHADER_TEXTURE_H_
